
 const { send } = require('process')
const bd =require('../bd/index')
//  const {md5} =require('md5')
const {setToken} = require('../jwt')
// const JwtUtil =require('../jwt')
// const tokenget =require('../setting')
const { sign } = require('jsonwebtoken');
const secret = 'demo';
  const zhuce= (req,res)=>{
 //获取前端发送的参数
 let {username,password,nickname,age} = req.body
 console.log(username,password,nickname,age);
 if(!username|| !password || !nickname) return  res.send({code:401,msg:'请填写账号密码和昵称'})
//  password=md5(`${password}`)
 //根据username 账号去数据库查找是否存在
 let index =null
 
  bd.query('SELECT * FROM userinfo ',(err,er)=>{
index = er.some(item =>item.user_name == username)
if(index===false ){
    //密码和密钥通过拼接
 //    password=md5(`${password}${PWD}`)
    //插入数据
    bd.query('insert into userinfo(user_name,user_password,nickname,user_age) value(?,?,?,?)',[username,password,nickname,age])
    res.send({code:200,msg:'注册成功'})
  }else{
    res.send({code:-1,msg:'该账号已存在'})
  }
})

}

// 登录
const login = (req,res)=>{
 //获取前端传过来的参数
 let {username,password} = req.body
//  password=md5(`${password}`)
let index =null
  bd.query('SELECT * FROM userinfo ',(err,er)=>{
  
index = er.findIndex(item =>(item.user_name == username && item.user_password == password) )
if(index == -1){
    res.send({code:401,msg:'账号或者密码错误'})
  }else{  
    const token = sign({ username }, secret, { expiresIn: '1h' });
// console.log(tokenget(token));

    bd.query("insert into token (token) values(?)", [token], (err, re) => {
      if(err){
        send({
          code:500,
          msg:'服务器错误'
        })
      }else{

      }

    })
    if(er[index].user_name ==='18163104359'){
      res.send({
        code:200,
        msg:'登录成功',
        token,
      data:{nickname:er[index].nickname,
        xuanqian:'*-*-*',
        rous:[
         
          {
            title: '上传吉他谱',
            path: '/addheadHeadkey'
        
          },
          {
            title: '工人信息',
            path: '/workerInformation'
        
          },
        
        
        ],
        id:er[index].user_id,
      
      }
    })
    }else{
      bd.query("insert into token (token) values(?)", [token], (err, re) => {
        if(err){
          send({
            code:500,
            msg:'服务器错误'
          })
        }else{
        }
      })
      res.send({
        code:200,
        msg:'登录成功',
        token,
      data:{nickname:er[index].nickname,
        id:er[index].user_id,
        rous:[
          
        
          {
            title: '上传吉他谱',
            path: '/addheadHeadkey'
        
          },
          
     
      ],
        xuanqian:'*',
       
      }
    })
    }
  }
  })
  
  

 }
 // 修改密码
 const ediltPass=(req,res)=>{
  let {lodPassword,newPassword,id,nickname} = req.body
  bd.query(`SELECT * FROM userinfo where user_id = '${id}'`,(err,re)=>{
    if(re[0].user_password === lodPassword){
      bd.query(`update userinfo set nickname ='${nickname}', user_password = '${newPassword}'  where user_id= ${id}`,(err,re)=>{
        if(err){
          res.send({
            code:500,
            msg:err.sqlMessage,
          })
        }else{
          res.send({
            code:200,
            msg:'修改成功',
            data:{nickname}
          })
        }
      })
    }else{
      res.send({
        code:402,
        msg:'旧密码错误'
      })
    }
  })
  // bd.query(`update userinfo set user_password = ${newPasswored}  where user_id= ${id}`)
 }
 module.exports=  {login,zhuce,ediltPass}
